package com.googlecode.lohr.parser.combinators.matcher.common;


import com.googlecode.lohr.parser.combinators.MatchError;
import com.googlecode.lohr.parser.combinators.matcher.MatchContext;
import com.googlecode.lohr.parser.combinators.matcher.Matcher;
import com.googlecode.lohr.parser.combinators.node.StringNode;

public class IntegerMatcher
extends Matcher
{
	@Override
	public void match(MatchContext ctx, int start) 
	{
		final String input= ctx.getInput();
		int i= start;
		if (start < input.length())
		{
			char c= input.charAt(start);
			int f= (c == '+' || c == '-') ? 1 : 0;
			
			if (Character.isDigit(input.charAt(f)))
			{
				i+= f + 1;
				while (Character.isDigit(input.charAt(i)))
					i++;
			}
		}
		
		if (i == start) {
			ctx.matchError(this, start, new MatchError("Expected an integer", start));
		}
		else {
			StringNode node= new StringNode(this, start, i, input.substring(0, i));
			ctx.matchFound(this, start, node);
		}
		ctx.matchComplete(this, start);
		
	}
	
	@Override
	public String getLabel()
	{
		return "integer";
	}
}
